package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domin.pojo.Student;
import com.itheima.domin.query.StudentQuery;
import com.itheima.domin.vo.PageResult;
import com.itheima.exception.ClientException;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**学生业务处理
 * @Author:上官徵羽
 * @Date: 2025/07/11 20:51
 * @Description TODO 今日内容:
 */
@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /** 学生查询列表分页查询 */
    @Override
    public PageResult<Student> findAll(StudentQuery studentQuery) {
        log.info("StudentServiceImpl findAll:{}",studentQuery);
        PageHelper.startPage(studentQuery.getPage(),studentQuery.getPageSize());
        Page<Student> p = studentMapper.findAll(studentQuery);
        return new PageResult<>(p.getTotal(),p.getResult());

    }

    /** 学员批量删除模块 */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStudentIds(Integer[] ids) {
        studentMapper.deleteStudentIds(ids);

    }

    /** 添加学生模块 */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addStudent(Student student) {
        //身份证判重
        LocalDateTime now = LocalDateTime.now();
        student.setCreateTime(now);
        studentMapper.addStudent(student);
    }

    /** 根据ID查询学生信息 */
    @Override
    public Student selectStudentId(Integer id) {
        return studentMapper.selectStudentId(id);
    }

    /** 修改学员信息模块 */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStudent(Student student) {
        Student student1 = studentMapper.selectStudentId(student.getId());
        //学号判重
        if (!StrUtil.equals(student1.getNo(),student.getNo())){
            Integer i = studentMapper.countNo(student.getNo());
            if (i>0){
             throw new ClientException(student.getNo()+"学号重复,请重新填写");
            }
        }
        //手机号判重
        if (!StrUtil.equals(student1.getPhone(),student.getPhone())){
            Integer i = studentMapper.countPhone(student.getPhone());
            if (i>0){
                throw new ClientException(student.getPhone()+"学号重复,请重新填写");
            }
        }
        //身份证号判重
        if (!StrUtil.equals(student1.getIdCard(),student.getIdCard())){
            Integer i = studentMapper.countIdCard(student.getIdCard());
            if (i>0){
                throw new ClientException(student.getIdCard()+"学号重复,请重新填写");
            }
        }
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    /** 违纪扣分模块 */
    @Override
    public void disciplinaryDisposition(Integer score,Integer id) {
        //判定分数是否正常
        if (score<0||score>100){
            throw new ClientException("请输入合适分数");
        }
        Student student = studentMapper.selectStudentId(id);
        //违纪次数
        student.setViolationCount(student.getViolationCount()+1);
        //违纪分数
        student.setViolationScore(student.getViolationScore()+score);

        log.info("StudentService disciplinaryDisposition:违纪分数:{} 违纪次数:{} id:{}",student.getViolationScore(),
                student.getViolationCount(),id);
        studentMapper.disciplinaryDisposition(student.getViolationScore(),
                student.getViolationCount(),id);
    }
}
